package com.gqz.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gqz.domain.SysUser;
import com.gqz.mapper.SysUserMapper;
import com.gqz.query.BaseQuery;
import com.gqz.service.ISysUserService;
import com.gqz.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public PageList<SysUser> getAllUser(BaseQuery query) {

        Example example = new Example(SysUser.class);
        if (query.getKeyword() != null) {
            example.or().orLike("userName", "%" + query.getKeyword() + "%");
            example.or().orLike("realName", "%" + query.getKeyword() + "%");
        }

        PageHelper.startPage(query.getPageIndex(), query.getPageSize());

        PageInfo<SysUser> sysUserPageInfo = new PageInfo<>(sysUserMapper.selectByExample(example));

        PageList<SysUser> list = new PageList<>(sysUserPageInfo.getTotal(), sysUserPageInfo.getList());

        return list;
    }

    @Override
    public boolean deleteUser(Long id) {
        return sysUserMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean batchDeleteUser(Long[] ids) {
        return sysUserMapper.batchDeleteUser(ids) > 0;
    }

    @Override
    public boolean updateUser(SysUser user) {
        return sysUserMapper.updateByPrimaryKey(user) > 0;
    }

    @Override
    public boolean insertUser(SysUser user) {
        String pwd = SecureUtil.md5(user.getPassword());
        user.setPassword(pwd);//将添加进来的密码进行md5加密
        return sysUserMapper.insert(user) > 0;
    }


}
